<?php
namespace  common\components\services\orders;
use common\components\helpers\Fun;
use frontend\models\datas\ClassManage;
use frontend\models\datas\ClassStudent;
use frontend\models\datas\Goods;
use frontend\models\datas\GoodsAttr;
use frontend\models\datas\Orders;
use frontend\models\datas\OrdersItems;
use frontend\models\datas\OrdersLog;
use frontend\models\datas\OrdersType;
use common\models\datas\Student;
use yii\base\Component;
use yii\base\ErrorException;
use yii\db\Exception;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/27
 * Time: 16:41
 */
class Oper extends Component
{
    /**
     * 订单操作记录
     * @param $option
     */
    public static function addOrderLog($option){
        $orders = Orders::findOne($option['orders_id']);

         $log = new OrdersLog();
        $log->orders_id = $option['orders_id'];
        $log->operator_id = \Yii::$app->user->id;
        $log->operator_name = \Yii::$app->user->identity->name;
        $log->status = $option['status'];
        $log->result = $option['result'];
        $log->log_text = $option['log_text'];
        $log->alttime = time();
        $log->save(false);
    }

    /**
     * 单个商品插入orderitem
     * @param object $model
     * @param object $goods
     * @param object $customer
     */
    public static function perfectOrder($model,$goods,$customer){
            $attrs = GoodsAttr::find()->where(['goods_id' => $goods->id])->asArray()->all();
            $student = Student::find()->where(['customer_id' => $customer->id])->one();
            //插入订单类型
            $orderType = new OrdersType();
            $orderType->orders_id = $model->id;
            $orderType->type_id = $goods->type_id;
            $orderType->amount = $model->final_amount;
            $orderType->quantity = 1;
            if($orderType->save(false)) {
                //插入订单课程
                $orderItem = new OrdersItems();
                $orderItem->orders_id = $model->id;
                $orderItem->orders_type_id = $orderType->id;
                $orderItem->goods_id = $goods->id;
                $orderItem->mechanic_id = $goods->mechanic_id;
                $orderItem->sn = $goods->sn;
                $orderItem->name = $goods->name;
                $orderItem->cost = $goods->cost;
                $orderItem->price = $goods->price;
                $orderItem->market_price = $goods->market_price;
                $orderItem->amount = $goods->price * 1;
                $orderItem->give_points = $goods->score;
                $orderItem->nums = 1;
                $orderItem->goods_attr = serialize($attrs);
                $orderItem->student_id = $student->id;
                $orderItem->cash_money = $model->final_amount;
                $orderItem->actual_cash_money = $model->final_amount;
                if($orderItem->save(false)){
                    if($goods->type_id == 1) {
                        $class = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $goods->id])->one();
                        $ClassStudentModel = new ClassStudent();
                        $ClassStudentModel->class_manage_id = $class->class_id;
                        $ClassStudentModel->order_sales_id = $orderItem->id;
                        $ClassStudentModel->student_id = $student->id;
                        $ClassStudentModel->save(false);
                    }
                }
            }

    }

    /**
     * 多个商品插入ordersitem
     * @param object $model
     * @param array $gids 商品id
     * @param object $customer
     */
    public static function perfectOrders($model,$gids,$customer,$orderType = ''){
        $student = Student::find()->where(['customer_id' => $customer->id])->one();
        if($gids){
            $discount_amount = $model->discount_amount/$model->total_amount; //计算优惠多少钱
            foreach($gids as $id){
                $attrs = GoodsAttr::find()->where(['goods_id' => $id])->asArray()->all();
                $goods = Goods::findOne($id);
                $orderType = OrdersType::find()->where(['orders_id'=>$model->id,'type_id'=>$goods->type_id])->one();
                if($orderType != null){
                    $orderType->quantity += 1;
                    $orderType->amount += ($goods->price)-($goods->price*$discount_amount);
                }else{
                    $orderType = new OrdersType();
                    $orderType->orders_id = $model->id;
                    $orderType->type_id = $goods->type_id;
                    $orderType->amount = ($goods->price)-($goods->price*$discount_amount);
                    $orderType->quantity = 1;
                }
                if($orderType->save(false)){
                    //插入订单课程
                    $orderItem = new OrdersItems();
                    $orderItem->orders_id = $model->id;
                    $orderItem->orders_type_id = $orderType->id;
                    $orderItem->goods_id = $goods->id;
                    $orderItem->mechanic_id = $goods->mechanic_id;
                    $orderItem->sn = $goods->sn;
                    $orderItem->name = $goods->name;
                    $orderItem->cost = $goods->cost;
                    $orderItem->price = $goods->price;
                    $orderItem->market_price = $goods->market_price;
                    $orderItem->amount = $goods->price * 1;
                    $orderItem->give_points = $goods->score;
                    $orderItem->nums = 1;
                    $orderItem->goods_attr = serialize($attrs);
                    $orderItem->student_id = $student->id;

                    $price = ($goods->price)-($goods->price*$discount_amount); //优惠后的单价
                    $total = $model->final_amount; //实付金额
                    $proportion = $price/$total; //计算比例

                    $orderItem->scholarship_money = ceil($proportion*$model->scholarship_money); //使用奖学金金额
                    $orderItem->actual_scholarship_money = ceil($proportion*$model->scholarship_money); //最终使用奖学金金额
                    $orderItem->cash_money = ceil($proportion*$model->cash_money); //使用余额金额
                    $orderItem->actual_cash_money = ceil($proportion*$model->cash_money); //最终使用余额金额
                    if($orderItem->save(false)) {
                        if($goods->type_id == 1) {
                            $class = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $goods->id])->one();
                            $ClassStudentModel = new ClassStudent();
                            $ClassStudentModel->class_manage_id = $class->class_id;
                            $ClassStudentModel->order_sales_id = $orderItem->id;
                            $ClassStudentModel->student_id = $student->id;
                            $ClassStudentModel->save(false);
                        }
                        $goods->store -= 1;
                        $goods->save(false);
                    }
                }
            }
        }

    }


}